<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Here are ALL price-calculate functions
 * tax, price incl. tax, price without tax
 * format price in german-standard (used as parameter!)
 * to show prices in the category- and articlelists
 *
 * used from article.php
 * 
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2013
 * @author     Norbert Gebert
 * @license    GPL3
 */

/**
 * Get the single price from an article incl. tax
 * @param  string $id   article-price-id
 * @return array  returns array with price and text
 */
function getSingleArticlePriceWithTax($id, $taxId = 0)
{
	$cols = Array('price_art', 'price_per_item', 'price_fix', 'price_list', 'price_unit');
	db::getInstance()->where('price_id', $id);
	$resulty = db::getInstance()->getOne("price", null, $cols);

	$product = array();
	switch ($resulty['price_art'])
	{
		case '0': // free
			$product = array('productPrice' => 0,
			 'productUnit' => _('This article is free!'),
			 'seasonPriceList' => '',
			 'priceTyp' => 0,
			 'productJs' => '');
			break;
		case '1': // price * item
			$product = array('productPrice' => number_format($resulty['price_per_item'], 2, ',',
				 '.'),
			 'productUnit' => _('Price per item'),
			 'seasonPriceList' => '',
			 'priceTyp' => 1,
			 'productJs' => '');
			// Price per 5 piece
			break;
		case '2': // (price * item ) + fixCosts
			$taxValue = getArticleTax($taxId);
			$product = array('productPrice' => number_format($resulty['price_per_item'] + $resulty['price_fix'],
				 2, ',', '.'),
			 'productUnit' => _('Price per item'),
			 'seasonPriceList' => '',
			 'priceTyp' => 2,
			 'productJs' =>
			 '<script type="text/javascript">' .
			 'function newPrice() { ' .
			 'taxValue = ' . $taxValue['productTaxValue'] . '; ' .
			 'numberx = document.getElementById("number").value;' .
			 'if (isNaN(parseInt(numberx))) {' .
			 'numberx = "1";' .
			 'document.getElementById("number").innerHTML = (numberx);' .
			 '}' .
			 'pricex = ' . $resulty['price_per_item'] . ' * parseInt(numberx) + ' . $resulty['price_fix'] . ';' .
			 'pricey = round(pricex/parseInt(numberx));' .
			 'taxy = round(((pricex/parseInt(numberx))/(taxValue+100))*taxValue);' .
			 'document.getElementById("sprice").innerHTML = (pricey);' .
			 'document.getElementById("stax").innerHTML = (taxy);' .
			 '} ' .
			 'function round(x) {' .
			 'var k = (Math.round(x * 100) / 100).toString();' .
			 'k += (k.indexOf(\'.\') == -1)? \'.00\' : \'00\'; ' .
			 'var p = k.indexOf(\'.\'); ' .
			 'return k.substring(0, p) + \',\' + k.substring(p+1, p+3);' .
			 '} </script>');
			break;
		case '3': // pricelist with graduated prices
			$taxValue = getArticleTax($taxId);
			$rows = explode("\r\n", $resulty['price_list']);

			foreach ($rows as $row)
			{
				$values = explode(':', $row);
				$value = explode('-', $values[0]);

				if (1 >= intval($value [0]) and 1 <= intval($value[1]))
				{
					//$pricex = str_replace(',', '.', $values[1]);
					$product = array('productPrice' =>
					 number_format(str_replace(',', '.', $values[1]), 2, ',', '.'),
					 'productUnit' => _('Price per item'),
					 'seasonPriceList' => $resulty['price_list'],
					 'priceTyp' => 3,
					 'productJs' => '');
				}
			}
			break;
	}
	return $product;
}

function

getArticlePriceWithoutTax($id)
{
	return TRUE;
}

/**
 * Return price-tax and tax-value as array
 * @param  string $id		article-tax-id
 * @param  string $price	article-price
 * @return array  returns array with productTax and productTaxValue
 */ 
function getArticleTax($id, $price = 1)
{
	$tax = array();

	if ($price > 0)
	{ //only tax if article not free!
		$cols = Array('tax_value');
		db::getInstance()->where('tax_id', $id);
		$productTaxValuex = db::getInstance()->getOne("tax", null, $cols);
		$productTaxValue = $productTaxValuex['tax_value'];

		$tax = array('productTaxValue' => $productTaxValue,
		 'productTax' => number_format(($price / (100 + $productTaxValue) * $productTaxValue),
			 2, ',', '.'));
	}
	else
	{
		$product[] = array('productTaxValue' => ' ', 'productTax' => ' ');
	}
	return $tax;
}

/**
 * Return price
 * @param  string $number	number of article
 * @param  string $price	article-price
 * @return string total price incl. tax
 */
function getTotalPriceWithTax($number, $price)
{

	$pricex = str_replace(',', '.', $price);
	return number_format($pricex * $number, 2, ',', '.');
}

/**
 * Returns total-tax for given total-price
 * @param array  $taxId			Tax-id 
 * @param array  $priceTotal	given Price 
 */
function getTotalPriceTax($taxId, $priceTotal)
{
	$cols = Array('tax_value');
	db ::getInstance()->where('tax_id', $taxId);
	$row = db::getInstance()->getOne("tax", null, $cols);
	$productTaxValue = $productTaxValuex['tax_value'];
	$taxx = $row['tax_value'];
	$pricex = str_replace(',', '.', $priceTotal);

	return number_format((($pricex / ($taxx + 100)) * $taxx), 2, ',', '.');
}
?>